package com.atguigu.java.ai.langchain4j.config;

import com.atguigu.java.ai.langchain4j.store.MongoChatMemoryStore;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.service.MemoryId;
import dev.langchain4j.store.memory.chat.InMemoryChatMemoryStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author huoyz
 * @Date 2025/5/9 13:58
 * @Version 1.0
 * @description: null
 *
 * chatMemoryStore 是自己通过MongoChatMemoryStore写的持久化操作
 */
@Configuration
public class SeparateMemoryChatAssistantConfig {
    @Autowired
    MongoChatMemoryStore memoryStore;
    @Bean
    ChatMemoryProvider separateChatMemory(){
        return memoryId ->
                MessageWindowChatMemory
                        .builder()
                        .id(memoryId)
                        .maxMessages(10)
                        // .chatMemoryStore(new InMemoryChatMemoryStore())  //这样聊天记忆就可以以键值对hashMap的形式存在内存中
                        .chatMemoryStore(memoryStore)   //持久化到mongodb
                        .build();
    }
}

//通过这个chatMemoryStore(new InMemoryChatMemoryStore()) 我们可以自己写一个接口，然后配置到此处实现自定义聊天管理，例如实例化存储

/* 要实现下面的一些方法
* public interface ChatMemoryStore {
    List<ChatMessage> getMessages(Object var1);

    void updateMessages(Object var1, List<ChatMessage> var2);

    void deleteMessages(Object var1);
}*/